package com.yuanheng100.ems4j.mapper;

import com.yuanheng100.ems4j.model.Ems4jMenu;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.caches.ehcache.EhcacheCache;

import java.util.List;

@CacheNamespace(implementation = EhcacheCache.class)
public interface Ems4jMenuMapper
{
    /**
     * 通过应用ID和角色ID获取所有的菜单
     *
     * @param appId
     * @param roleId
     * @return
     */
    @SelectProvider(type = Ems4jMenuSQLProvider.class, method = "selectMenuListByAppIdAndRoleId")
    List<Ems4jMenu> selectMenuListByAppIdAndRoleId(@Param("appId") int appId, @Param("roleId") int roleId);

    /**
     * 增加一条菜单项
     *
     * @param record
     * @return
     */
    @Insert({
            "INSERT INTO ems4j_menu (menuid, appid, ",
            "parentid, menuname, ",
            "url, ordernum, ",
            "permission, description, ",
            "menutype)",
            "VALUES (#{menuid,jdbcType=INTEGER}, #{appid,jdbcType=INTEGER}, ",
            "#{parentid,jdbcType=INTEGER}, #{menuname,jdbcType=VARCHAR}, ",
            "#{url,jdbcType=VARCHAR}, #{ordernum,jdbcType=SMALLINT}, ",
            "#{permission,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, ",
            "#{menutype,jdbcType=SMALLINT})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "menuId")
    int insert(Ems4jMenu record);

    /**
     * 更新一条菜单
     *
     * @param record
     * @return
     */
    @Update({
            "UPDATE ems4j_menu",
            "SET appid = #{appid,jdbcType=INTEGER},",
            "parentid = #{parentid,jdbcType=INTEGER},",
            "menuname = #{menuname,jdbcType=VARCHAR},",
            "url = #{url,jdbcType=VARCHAR},",
            "ordernum = #{ordernum,jdbcType=SMALLINT},",
            "permission = #{permission,jdbcType=VARCHAR},",
            "description = #{description,jdbcType=VARCHAR},",
            "menutype = #{menutype,jdbcType=SMALLINT}",
            "WHERE menuid = #{menuid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Ems4jMenu record);

    /**
     * 某个角色增加菜单
     *
     * @param menuId
     * @param roleId
     * @return
     */
    @Insert({
            "INSERT INTO ems4j_role_menu(roleId,menuId)",
            "VALUES(#{roleId,jdbcType=INTEGER}, #{menuId,jdbcType=INTEGER})"
    })
    int addMenuToRole(@Param("menuId") int menuId, @Param("roleId") int roleId);

    /**
     * 某个角色移除菜单
     *
     * @param menuId
     * @param roleId
     * @return
     */
    @Delete({
            "DELETE FROM ems4j_role_menu",
            "WHERE menuId = #{menuId,jdbcType=INTEGER} AND roleId = #{roleId,jdbcType=INTEGER}"
    })
    int removeMenuFromRole(@Param("menuId") int menuId, @Param("roleId") int roleId);

    /**
     * 删除ems4j_role_menu表中与menuId关联项
     *
     * @param menuId
     * @return
     */
    @Delete({
            "DELETE FROM ems4j_role_menu WHERE menuId = #{menuId,jdbcType=INTEGER}"
    })
    int deleteRoleMenuByMenuId(int menuId);

    /**
     * 删除菜单
     *
     * @param menuId
     * @return
     */
    @Delete({
            "DELETE FROM ems4j_menu",
            "WHERE menuId = #{menuId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer menuId);

    @Select({
            "SELECT",
            "menuid, appid, parentid, menuname, url, ordernum, permission, description, menutype",
            "FROM ems4j_menu",
            "WHERE menuid = #{menuid,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "menuid", property = "menuid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "appid", property = "appid", jdbcType = JdbcType.INTEGER),
            @Result(column = "parentid", property = "parentid", jdbcType = JdbcType.INTEGER),
            @Result(column = "menuname", property = "menuname", jdbcType = JdbcType.VARCHAR),
            @Result(column = "url", property = "url", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ordernum", property = "ordernum", jdbcType = JdbcType.SMALLINT),
            @Result(column = "permission", property = "permission", jdbcType = JdbcType.VARCHAR),
            @Result(column = "description", property = "description", jdbcType = JdbcType.VARCHAR),
            @Result(column = "menutype", property = "menutype", jdbcType = JdbcType.SMALLINT)
    })
    Ems4jMenu selectByPrimaryKey(Integer menuId);
}